package com.chinatechstar.notification.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.chinatechstar.component.commons.result.ActionResult;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.validator.InsertValidator;
import com.chinatechstar.component.commons.validator.UpdateValidator;
import com.chinatechstar.notification.entity.Notification;
import com.chinatechstar.notification.service.NotificationService;
import com.chinatechstar.notification.vo.NotificationVO;

/**
 * 消息通知的控制层
 */
@Api(value = "消息通知的控制层", tags = {"消息通知"})
@RestController
@RequestMapping("/notification")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    /**
     * 查询消息通知分页
     *
     * @param notificationVO 消息通知前端参数
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询消息通知分页", tags = {"消息通知"})
    @GetMapping(path = "/queryNotification")
    public ListResult<Object> queryNotification(NotificationVO notificationVO) {
        Map<String, Object> data = notificationService.queryNotification(notificationVO.getCurrentPage(), notificationVO.getPageSize(),
                notificationVO.getTitle(), notificationVO.getContent(), notificationVO.getType(), null, notificationVO.getSorter());
        return ResultBuilder.buildListSuccess(data);
    }

    /**
     * 查询我的消息分页
     *
     * @param notificationVO 我的消息前端参数
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询我的消息分页", tags = {"消息通知"})
    @GetMapping(path = "/queryMyNotification")
    public ListResult<Object> queryMyNotification(NotificationVO notificationVO) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Map<String, Object> data = notificationService.queryNotification(notificationVO.getCurrentPage(), notificationVO.getPageSize(),
                notificationVO.getTitle(), notificationVO.getContent(), notificationVO.getType(), authentication.getName(), notificationVO.getSorter());
        return ResultBuilder.buildListSuccess(data);
    }

    /**
     * 查询顶部消息通知的数据
     *
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询顶部消息通知的数据", tags = {"消息通知"})
    @GetMapping(path = "/queryHeadNotification")
    public ListResult<Object> queryHeadNotification() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        List<LinkedHashMap<String, Object>> data = notificationService.queryHeadNotification(authentication.getName());
        return ResultBuilder.buildListSuccess(data);
    }

    /**
     * 查询类型的下拉框数据
     *
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询类型的下拉框数据", tags = {"消息通知"})
    @GetMapping(path = "/queryNotificationType")
    public ListResult<Object> queryNotificationType() {
        List<LinkedHashMap<String, Object>> data = notificationService.queryNotificationType();
        return ResultBuilder.buildListSuccess(data);
    }

    /**
     * 查询优先级的下拉框数据
     *
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询优先级的下拉框数据", tags = {"消息通知"})
    @GetMapping(path = "/queryNotificationPriority")
    public ListResult<Object> queryNotificationPriority() {
        List<LinkedHashMap<String, Object>> data = notificationService.queryNotificationPriority();
        return ResultBuilder.buildListSuccess(data);
    }

    /**
     * 新增消息通知
     *
     * @param notification 消息通知对象
     * @param username     发布对象的用户名
     * @return ActionResult
     */
    @ApiOperation(value = "新增消息通知", tags = {"消息通知"})
    @PostMapping(path = "/addNotification")
    public ActionResult addNotification(@Validated(InsertValidator.class) @RequestBody Notification notification,
                                        @RequestParam(name = "username", required = false) String[] username) {
        notificationService.insertNotification(notification, username);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 编辑消息通知
     *
     * @param notification 消息通知对象
     * @param username     发布对象的用户名
     * @return ActionResult
     */
    @ApiOperation(value = "编辑消息通知", tags = {"消息通知"})
    @PutMapping(path = "/updateNotification")
    public ActionResult updateNotification(@Validated(UpdateValidator.class) @RequestBody Notification notification,
                                           @RequestParam(name = "username", required = false) String[] username) {
        notificationService.updateNotification(notification, username);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 删除消息通知
     *
     * @param id 消息通知ID
     * @return ActionResult
     */
    @ApiOperation(value = "删除消息通知", tags = {"消息通知"})
    @PostMapping(path = "/deleteNotification")
    public ActionResult deleteNotification(@RequestParam(name = "id") Long[] id) {
        notificationService.deleteNotification(id);
        return ResultBuilder.buildActionSuccess();
    }

}
